package com.zlq.cn.service.exam;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.zlq.cn.dto.param.GenTestPaper;
import com.zlq.cn.entity.ExamChapters;
import com.zlq.cn.entity.ExamQuestions;
import com.zlq.cn.mapper.exam.ExamChaptersMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import one.util.streamex.StreamEx;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhoucl
 * @since 2024-04-10
 */
@Service
public class ExamChaptersService extends ServiceImpl<ExamChaptersMapper, ExamChapters> {

    @Resource
    private ExamQuestionsService examQuestionsService;

    public Collection<List<ExamQuestions>> genTestPaper(GenTestPaper genTestPaper) {
        List<GenTestPaper.GenTestPaperConfig> configs = new ArrayList<>();
        int num = genTestPaper.getNum();
        for (int i = 0; i < num; i++) {
            for (GenTestPaper.GenTestPaperConfig genTestPaperConfig : genTestPaper.getConfig()) {
                GenTestPaper.GenTestPaperConfig newConfig = BeanUtil.copyProperties(genTestPaperConfig, GenTestPaper.GenTestPaperConfig.class);
                newConfig.setChapterId(genTestPaper.getChapterId());
                newConfig.setTestPaperNum(i);
                configs.add(newConfig);
            }
        }
        List<ExamQuestions> examQuestions = baseMapper.randQuestions(configs);
        examQuestionsService.bindAnswers(examQuestions, null);
        return StreamEx.of(examQuestions)
                .groupingBy(ExamQuestions::getTestPaperNum)
                .values();
    }
}
